package com.battle.project.order.mq.send;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderSendMqHandler {

    private static Logger logger = LoggerFactory.getLogger(OrderSendMqHandler.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(String exchangeName, String routingKey, String message) {
        try {
            if (StringUtils.isBlank(message)) {
                return;
            }
            rabbitTemplate.convertAndSend(exchangeName, routingKey, message);
            logger.info("OrderSendMqHandler发送消息：{}", message);
        } catch (Exception e) {
            logger.info("OrderSendMqHandler发送mq消息出现异常", e);
        }
    }

    public void sendHeadersMessage(String exchangeName, String routingKey, Message message) {
        try {
            if (StringUtils.isBlank(exchangeName)) {
                return;
            }
            rabbitTemplate.send(exchangeName, routingKey, message);

            logger.info("OrderSendMqHandler发送消息：{}", message);
        } catch (Exception e) {
            logger.info("OrderSendMqHandler发送mq消息出现异常", e);
        }
    }
}
